# 从sentence_transformers库中导入CrossEncoder类
from sentence_transformers import CrossEncoder

# 加载预训练的Cross-Encoder模型，用于句子对相关性评分
rerank_model = CrossEncoder("cross-encoder/ms-marco-MiniLM-L-6-v2")

# 构造待评估的句子对列表，每个元素是一个(query, document)元组
sentence_pairs = [
    [
        "How many people live in Berlin?",
        "Berlin has a population of 3,520,031 registered inhabitants in an area of 891.82 square kilometers.",
    ],
    ("How many people live in Berlin?", "Berlin is well known for its museums."),
]

# 使用Cross-Encoder模型对句子对进行相关性打分，返回分数列表
scores = rerank_model.predict(sentence_pairs)

# 打印相关性分数数组 [ 8.845852 -4.320078]
print(scores)

# 将句子对和分数打包成一个zip对象（迭代器） <zip object at 0x000001FE559525C0>
zipped = zip(sentence_pairs, scores)

# 打印zip对象本身（显示为对象地址）
print(zipped)

# 将zip对象转换为列表并打印，显示每个句子对及其分数
print(list(zipped))

# 逐个遍历句子对和对应分数，格式化输出每一对及其相关性分数
for pair, score in zip(sentence_pairs, scores):
    print(f"{pair} => Score: {score:.4f}")

# names = ['Alice', 'Bob', 'Charlie']
# ages = [25, 30, 35]

# zipped = zip(names, ages)
# print(list(zipped))
# # 输出: [('Alice', 25), ('Bob', 30), ('Charlie', 35)]
